package com.hake.core.support.web;

import java.util.HashMap;
import java.util.Map;


/**
 * 公共参数
 * @author yhaoquan
 *
 */
public class PageParamsThreadLocal {

	/**
	 * 页码
	 */
	private static ThreadLocal<Integer> pageNum = new ThreadLocal<Integer>();

	/**
	 * 每页显示几条
	 */
	private static ThreadLocal<Integer> pageSize = new ThreadLocal<Integer>();

	/**
	 * SQL 排序
	 */
	private static ThreadLocal<String> orderBy = new ThreadLocal<String>();
	
	/**
	 * 分页参数集合
	 * 固定参数：
	 * 		pageNum
	 * 		pageSize
	 * 		orderBy
	 */
	private static ThreadLocal<Map<String, Object>> pageParams = new ThreadLocal<Map<String, Object>>();

	public static Map<String, Object> getPageParams() {
		Map<String, Object> params = new HashMap<String, Object>() ;
		params.put("pageNum", getPageNum());
		params.put("pageSize", getPageSize());
		params.put("orderBy", getOrderBy());
		pageParams.set(params);
		return pageParams.get();
	}
	public static void removePageParams() {
		pageParams.remove();
	}
	
	public static Integer getPageNum() {
		if(null == pageNum.get()) setPageNum(1); ;
		return pageNum.get();
	}

	public static void setPageNum(Integer _pageNum) {
		pageNum.set(_pageNum);
	}
	
	public static void removePageNum() {
		pageNum.remove();
	}
	
	public static Integer getPageSize() {
		if(null == pageSize.get()) setPageSize(10);
		return pageSize.get();
	}
	
	public static void setPageSize(Integer _pageSize) {
		pageSize.set(_pageSize); 
	}
	
	public static void removePageSize() {
		pageSize.remove();
	}

	public static String getOrderBy() {
		if(null == orderBy.get()) setOrderBy("id desc");
		return orderBy.get();
	}

	public static void setOrderBy(String _orderBy) {
		orderBy.set(_orderBy);
	}

	public static void removeOrderBy() {
		orderBy.remove();
	}
	
	public static void removeAllPageParam() {
		removePageNum();
		removePageSize();
		removeOrderBy();
		removePageParams();
	}

}
